#include <iostream>
#include <pthread.h>
#include <string>
#include <unistd.h>
using namespace std;
// 锁的初始化
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// 条件变量
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void *worker(void *argc)
{
    string name = (const char*)argc;

    while (true)
    {
        pthread_mutex_lock(&mutex);
        //没有对于资源是否就绪的判定
        pthread_cond_wait(&cond,&mutex); // mutex???
        printf("%s is work\n",name.c_str());
        pthread_mutex_unlock(&mutex);
    }
    
}
int main()
{
    pthread_t tid1,tid2,tid3;
    pthread_create(&tid1,nullptr,worker,(void*)"thread-1-worker");
    pthread_create(&tid2,nullptr,worker,(void*)"thread-2-worker");
    pthread_create(&tid3,nullptr,worker,(void*)"thread-3-worker");

    sleep(3);
    printf("MAIN thread boss ctrl begin.....\n");

    while (true)
    {
        printf("MAIN thread boss look\n");
        pthread_cond_signal(&cond);
        // pthread_cond_broadcast(&cond);
        sleep(1);
    }
    

    pthread_join(tid1,nullptr);
    pthread_join(tid2,nullptr);
    pthread_join(tid3,nullptr);
    return 0;
}